import Vue from 'vue'
import getuser from '@/util/getuser'
import router from '@/router/index'
import config from '@/config/config'
import VueResource from 'vue-resource'
import Base64 from '@/util/Base64'
import checkSource from '@/util/checkSource'
import store from '@/store/index'
import {setCookie, getCookie} from '@/util/user'

Vue.use(VueResource);

function relogin() {
  var username= getuser().username;
  var pwd = getuser().pwd
  console.log('auto');

  if (username && pwd) {
    pwd = Base64.decode(getuser().pwd)
    Vue.http.post(config.baseUrl + '/app2_0/login.do', {
      'username': username,
      'password': pwd,
      'appStatus': '1',
      'dataSource': checkSource()
    }, {
      headers: {},
      credientials: true,
      emulateJSON: true
    }).then(function (response) {
      var data = response.data;
      console.log(response.data);
      if (data.return_code == 0) {
        //成功
        var user = {
          id: data.return_data.id,
          realName: data.return_data.realName,
          username: data.return_data.username,
          userType: data.return_data.userType,
          bankCode: data.return_data.bankCode,
          idNo: data.return_data.idNo,
          idType: data.return_data.idType,
          totalSum: data.return_data.totalSum,
          cellPhone: data.return_data.cellPhone,
          bankName: data.return_data.bankName,
          isSetPassword: data.return_data.isSetPassword,
          usableSum: data.return_data.usableSum,
          freezeSum: data.return_data.freezeSum,
          cashStatus: data.return_data.cashStatus,
          cardNo: data.return_data.cardNo,
          hsBankNoDisplay: data.return_data.hsBankNoDisplay,
          hsBankNo: data.return_data.hsBankNo,
          forPaySum: data.return_data.forPaySum,
          cardNoStar: data.return_data.cardNoStar,
          encryptUserID: data.return_data.encryptUserID,
          pwd: Base64.encode(pwd)
        }
        localStorage.setItem('zydUser', JSON.stringify(user))

        setCookie('id', data.return_data.id, 'd365')
        setCookie('realName', data.return_data.realName, 'd365')
        setCookie('username', data.return_data.username, 'd365')
        setCookie('userType', data.return_data.userType, 'd365')
        setCookie('bankCode', data.return_data.bankCode, 'd365')
        setCookie('idNo', data.return_data.idNo, 'd365')
        setCookie('idType', data.return_data.idType, 'd365')
        setCookie('totalSum', data.return_data.totalSum, 'd365')
        setCookie('cellPhone', data.return_data.cellPhone, 'd365')
        setCookie('bankName', data.return_data.bankName, 'd365')
        setCookie('isSetPassword', data.return_data.isSetPassword, 'd365')
        setCookie('usableSum', data.return_data.usableSum, 'd365')
        setCookie('freezeSum', data.return_data.freezeSum, 'd365')
        setCookie('cashStatus', data.return_data.cashStatus, 'd365')
        setCookie('cardNo', data.return_data.cardNo, 'd365')
        setCookie('hsBankNoDisplay', data.return_data.hsBankNoDisplay, 'd365')
        setCookie('hsBankNo', data.return_data.hsBankNo, 'd365')
        setCookie('forPaySum', data.return_data.forPaySum, 'd365')
        setCookie('cardNoStar', data.return_data.cardNoStar, 'd365')
        setCookie('encryptUserID', data.return_data.encryptUserID, 'd365')
        setCookie('pwd', Base64.encode(pwd), 'd365')

        if(/(Android)/i.test(navigator.userAgent)){
          router.go(0)
        }else{
          router.push({path: '/blank'})
        }

      } else {
        router.push({path: '/login'})
      }
    });

  } else {
    router.push({path: '/login'})
  }
}
export default relogin;
